<?php
namespace jy_peimilk;
defined('BY_JYA') or exit('error');
/*
  配送订单
	
*/
class order extends \plugin{
	/*
		系统指派，peisong_id是MID(未绑定配送员的订单才指派)
	*/
	public function order_pai($order){
		$order_id=intval($order['id']);
		$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid']);
		$peisong_id=$orderuser['peisong_id'];
		if(!$peisong_id){
			$allusers=array();
			if($order['merchid']){
				$areas=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid'],'merchid OR'=>'merchid=0 or merchid = \''.$order['merchid'].'\''));
				foreach($areas as &$item){
					$users=pdo_getall('jy_peimilk_member',array('level_id'=>$item['id'],'status'=>1),'mid');
					foreach($users as $uv){
						$allusers[]=$uv;
					}
				}
			}else{
				$users=pdo_getall('jy_peimilk_member',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
			$nums=count($allusers);
			$key=rand(0,$nums-1);
			$key=$key?:0;
			$peisong_id=$allusers[$key]['mid'];
			if(!$peisong_id){
				return false;
			}
			$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid'],$peisong_id);
		}
		$params=array('order_id'=>$order_id,'peisong_id'=>$peisong_id);
		$this->order_sendget($params);
		return true;
	}
	/*
		抢单peisong_id是MID,$iszd自动接单，如果客户绑定配送员的话
	*/
	public function order_get($params,$iszd=false,$ischeck=false){
		$order_id=intval($params['order_id']);
		$peisong_id=intval($params['peisong_id']);
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('status','is_dingpei','mid','merchid'));
		if(!$iszd){
			//兼容错误，传过来的是ID而不是MID的时候！
			$mid=pdo_getcolumn('jy_peimilk_member',array('mid'=>$peisong_id,'uniacid'=>$_SESSION['uniacid']),'mid');
			if(!$mid){
				$peisong_id=pdo_getcolumn('jy_peimilk_member',array('id'=>$peisong_id),'mid');
			}
		}
		$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid'],$peisong_id,$ischeck);
		$peisong_id=$orderuser['peisong_id'];
		if(!$peisong_id){
			return false;
		}
		$isget=$order['status'];
		if($isget==2){
			exi('该订单已被别人抢单了！','error');
		}
		pdo_update('jy_weishop_order',array('peisong_id'=>$peisong_id,'status'=>2,'send_status'=>1),array('id'=>$order_id));
		$is_dingpei=$order['is_dingpei'];
		if(!$is_dingpei){
			$this->create_sendlog($order_id,$peisong_id,1);
		}else{
			$params=array('orderid'=>$order_id,'uniacid'=>$_SESSION['uniacid']);
			$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('mid','merchid','istaocan','goodsid','optionid','orderid','price','total','goodsnum'));
			foreach($ordergoods as $k=>&$g){
					$goodskey=$g['goodsid'].'_'.$g['optionid'];
					$g['allnum']=$g['goodsnum']?$g['goodsnum']*$g['total']:$g['total'];
					$goods=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']));
					$g['unit']=$goods['unit'];
					$g['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$g['goodsid']),'weight');
					if($g['optionid']){
							$options=pdo_get('jy_weishop_goods_option',array('id'=>$g['optionid']),array('title','specs'));
							if($g['istaocan']==1){
									$specs_list=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'specs'=>''));
									$selespename = $specs_list[0]['title'];
							}else{
									$specs=$options['specs'];
									$g['price']=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'price');
									$specs=explode("_",$specs);
									foreach($specs as &$spec){
										$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
									}
									$optiontitle=implode("，",$specs);
									$selespename = $optiontitle;
							}
							$g['optiontitle']=$g['selespename']= $selespename;			
					}
					$g['title']=$goods['title'];
					$goodsset=$this->model('model')->check_goodsset($g);
			}
			$this->get_allgoods($order['mid'],$order['merchid']);
			$this->upallgoodssendlog($order['mid'],$order['merchid']);
		}
		model('jy_weishop/order')->order_send($order_id,$params,true);
		return true;
	}
	/*
		发送订单给配送员
	*/
	public function order_sendget($params){
		$order_id=intval($params['order_id']);
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id));
		$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid']);
		$peisong_id=$orderuser['peisong_id'];
		if(!$peisong_id){
			$peisong_id=intval($params['peisong_id']);
			//兼容错误，传过来的是ID而不是MID的时候！
			$mid=pdo_getcolumn('jy_peimilk_member',array('mid'=>$peisong_id,'uniacid'=>$_SESSION['uniacid']),'mid');
			if(!$mid){
				$peisong_id=pdo_getcolumn('jy_peimilk_member',array('id'=>$peisong_id),'mid');
			}
		}
		pdo_update('jy_weishop_order',array('peisong_id'=>$peisong_id,'status'=>2,'send_status'=>13),array('id'=>$order_id));
		$this->model('notice')->notice_orderget($order_id);
		return true;
	}
	/*
		配送员确定接单
	*/
	public function order_checkget($order_id){
		pdo_update('jy_weishop_order',array('peisong_id'=>$_SESSION['uid'],'status'=>2,'send_status'=>1),array('id'=>$order_id));
		$this->up_sendlog($order_id,$_SESSION['uid'],2);
		$this->model('notice')->notice_orderget($order_id);
		model('jy_weishop/order')->order_send($order_id,'',true);
		return true;
	}
	/*
		转单超时
	*/
	public function order_checktime($order_id=0){
		$setting=$this->plugin_setting('setting');
		$send_status13_time=intval($setting['send_status13_time']);
		if(!$send_status13_time){
			return false;
		}
		if($order_id){
			$sendlog=pdo_get('jy_peimilk_sendlog',array('status'=>0,'mid'=>$_SESSION['uid'],'order_id'=>$order_id));
			if($sendlog['createtime']){
				$lasttime=$sendlog['createtime']+$send_status13_time*60;
				if($lasttime<TIMESTAMP){
					pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
					$this->up_sendlog($order_id,$sendlog['mid'],3);
				}
			}
		}else{
			$sendlogs=pdo_getall('jy_peimilk_sendlog',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
			foreach($sendlogs as $sendlog){
				$order_id=$sendlog['order_id'];
				if($sendlog['createtime']){
					$setting=$this->plugin_setting('setting');
					$lasttime=$sendlog['createtime']+$send_status13_time*60;
					if($lasttime<TIMESTAMP){
						pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
						$this->up_sendlog($order_id,$sendlog['mid'],3);
					}
				}
			}
		}
		return true;
	}
	/*
		完成订单
	*/
	public function order_receive($order_id){
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('ordersn','price','is_dingpei'));
		$goodsnum=0;
		if($order['is_dingpei']){
			$item['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order_id));
			foreach($item['goods'] as &$g){
				if($goodsnum<$g['goodsnum']){
					$goodsnum=$g['goodsnum'];
				}
			}
			$issendnum=pdo_count('jy_peimilk_sendlog',array('status'=>2,'order_id'=>$order_id));
		}
		if($goodsnum<=$issendnum+1 || !$order['is_dingpei']){
			pdo_update('jy_weishop_order',array('send_status'=>4),array('id'=>$order_id));
			model('jy_weishop/order')->order_receive($order_id);	
		}
		$this->model('order')->up_sendlog($order_id,'',2);
		return true;
	}
	/*
		接单日志$order_id为空，表示定期配送
	*/
	public function create_sendlog($order_id,$peisong_id=0,$status=0,$day='',$sendtype='',$daygoods=array(),$buymid=0,$merchid=0,$is_dingpei=0){
		$peisong_id=$peisong_id?:$_SESSION['uid'];
		if($order_id){
			$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('merchid','sendtype','mid','is_dingpei'));
			$is_dingpei=$order['is_dingpei'];
			$peiset=$this->pdo_get('peiset',array('order_id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			$peisetgoods=json_decode($peiset['goods'],true);
			$num=0;
			if($peisetgoods){
				foreach($peisetgoods as $k=>$g){
					$num +=$g['num'];
				}
			}
		}
		$buymid=$buymid?:$order['mid'];
		$merchid=$merchid?:$order['merchid'];
		if($num<=0 && $is_dingpei && !$daygoods){
			exi('未配置任何商品和数量，不能发起配送！','error');
		}
		if($is_dingpei){
			$peiset=$this->pdo_get('peiset',array('mid'=>$buymid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
			$goods=$daygoods['goods'];
			if(!$daygoods['goods']){
				return false;
			}
		}else{
			$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order_id),array('goodsid','price','total'));
			foreach($goods as &$g){
				$g['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$g['goodsid']),'title');
				$g['num']=$g['total'];
			}
		}
		$data=array(
				'mid'=>$peisong_id,
				'sendtype'=>$sendtype?:$order['sendtype'],
				'day'=>$day,
				'daytime'=>strtotime($day),
				'times'=>$peiset['times'],
				'goods'=>ijson_encode($goods),
				'status'=>$status,
		);
		if($order_id){
			$islog=$this->pdo_get('sendlog',array('day'=>$day,'uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
		}else{
			$islog=$this->pdo_get('sendlog',array('day'=>$day,'uniacid'=>$_SESSION['uniacid'],'buymid'=>$buymid,'merchid'=>$merchid,'is_dingpei'=>1));
		}
		if($islog['id']){
			$this->update('sendlog',$data,array('id'=>$islog['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$merchid?:$order['merchid'];
			$data['order_id']=$order_id;
			$data['createtime']=TIMESTAMP;
			$data['buymid']=$buymid?:$order['mid'];
			$data['is_dingpei']=$is_dingpei;
			$this->insert('sendlog',$data);
		}
		return true;
	}
	public function upallgoodssendlog($mid,$merchid=0,$stopdays=''){
		$allgoodsset=$this->pdo_getall('goodsset',array('mid'=>$mid,'merchid'=>$merchid));
		//删除所有配送单
		$day=date('Y-m-d');
		$nowdaytime=strtotime($day)+100;
		$this->pdo_delete('sendlog',array('daytime >'=>$nowdaytime+24*3600-500,'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid'],'buymid'=>$mid,'merchid'=>$merchid,'is_dingpei'=>1));
		foreach($allgoodsset as $goodsset){
			$this->upsendlog($goodsset,$stopdays);
		}
		return true;
	}
	public function upsendlog($goodsset=array(),$stopdays='',$starday=''){
		$numset=json_decode($goodsset['numset'],true);
		$nums=$this->get_goodslognum($goodsset);
		$lastnum=$nums['lastnum'];
		$orderuser=$this->model('model')->up_orderuser($goodsset['mid'],$goodsset['merchid']);
		$peisong_id=$orderuser['peisong_id'];
		if($goodsset['merchid']){
			$yuesetting=$this->plugin_setting('yuesetting'.$goodsset['merchid']);
		}else{
			$yuesetting=$this->plugin_setting('yuesetting');
		}
		if(!$stopdays){
			$stopdays=$this->pdo_getcolumn('peiset',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$goodsset['mid'],'merchid'=>$goodsset['merchid']),'stopdays');
		}
		$no_weeks=$yuesetting['no_weeks'];
		$nopei_days=$yuesetting['nopei_days'];
		$day=$starday?:date('Y-m-d');
		$nowdaytime=strtotime($day)+100;
		for($i=1;$i<=365;$i++){
			$daytime=$nowdaytime+$i*24*3600;
			$week = date('w', $daytime);
			$day=date('Y-m-d',$daytime);
			if($lastnum<=0){
				break;
			}
			if(strpos($no_weeks,$week.';') !== false){
				continue;
			}
			if(strpos($nopei_days,$day.';') !== false){
				continue;
			}
			if($stopdays){
				if(strpos($stopdays,$day.';') !== false){
					continue;
				}
			}
			$daygoods=array('key'=>1);
			$sendlog=$this->pdo_get('sendlog',array('day'=>$day,'uniacid'=>$_SESSION['uniacid'],'buymid'=>$goodsset['mid'],'merchid'=>$goodsset['merchid'],'is_dingpei'=>1));
			$daygoods['goods']=json_decode($sendlog['goods'],true);
			foreach($numset as $lkey=>$nv){
				$data=$nv['data'];
				if($lastnum<=0){
					break;
				}
				if($data[$week]['num']>0){
						if($data[$week]['num']>$lastnum){
							$data[$week]['num']=$lastnum;
						}
						$lastnum-=$data[$week]['num'];
						$isadd=1;
						$key=$goodsset['goodsid'].'_'.$goodsset['optionid'].'_'.$lkey;
						$daygoods['goods'][$key]=array(
								'num'=>$data[$week]['num'],
								'title'=>$goodsset['title'],
								'lkey'=>$lkey,
								'leititle'=>$nv['title'],
								'goodsid'=>$goodsset['goodsid'],
								'optionid'=>$goodsset['optionid'],
								'selespename'=>$goodsset['selespename'],
						);
				}
			}
			if($daygoods['goods']){
				$this->create_sendlog(0,$peisong_id,0,$day,1,$daygoods,$goodsset['mid'],$goodsset['merchid'],1);
			}
		}
		return true;
	}
	public function up_delete($daytime=0,$goodsset=array()){
		$sendlogs=$this->pdo_getall('sendlog',array('daytime >='=>$daytime,'uniacid'=>$_SESSION['uniacid'],'buymid'=>$goodsset['mid'],'merchid'=>$goodsset['merchid'],'is_dingpei'=>1));
		foreach($sendlogs as $sv){
			$goods=json_decode($sv['goods'],true);
			$isup=0;
			foreach($goods as $k=>&$gv){
				if($goodsset['goodsid']==$gv['goodsid'] && $goodsset['optionid']==$gv['optionid']){
					unset($goods[$k]);
					$isup=1;
				}
			}
			if($isup){
				if(count($goods)>0){
					$this->pdo_update('sendlog',array('goods'=>ijson_encode($goods)),array('id'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']));
				}else{
					$this->pdo_delete('sendlog',array('id'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		return true;
	}
	public function upstopdays($stopdays='',$buymid=0,$merchid=0){
		if(!$stopdays){
			return false;
		}
		$stopdays=str_array($stopdays);
		$allgoods=array();
		$lastsendlog=$this->pdo_get('sendlog',array('status'=>array(0,1),'buymid'=>$buymid,'merchid'=>$merchid),'','daytime desc');
		if(!$lastsendlog){
			return false;
		}
		foreach($stopdays as $day){
			if(strtotime($day)+20*24*3600<time()){
				continue;
			}
			$sendlog=$this->pdo_get('sendlog',array('day'=>$day,'buymid'=>$buymid,'merchid'=>$merchid));
			$goods=$sendlog['goods']?json_decode($sendlog['goods'],true):array();
			foreach($goods as $k=>$gv){
				$key=$gv['goodsid'].'_'.$gv['optionid'];
				if(!$allgoods[$key]){
					$allgoods[$key]=$gv;
				}else{
					$allgoods[$key]['num'] +=$gv['num'];
				}
			}
			$this->pdo_delete('sendlog',array('id'=>$sendlog['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		$peisong_id=$lastsendlog['peisong_id'];
		if($merchid){
			$yuesetting=$this->plugin_setting('yuesetting'.$merchid);
		}else{
			$yuesetting=$this->plugin_setting('yuesetting');
		}
		if(!$lastsendlog['daytime']){
			$orderuser=$this->model('model')->up_orderuser($buymid,$merchid);
			$peisong_id=$orderuser['peisong_id'];
			$day=date('Y-m-d');
			$nowdaytime=strtotime($day)+100;
			$lastsendlog['daytime']=$nowdaytime;
		}
		$no_weeks=$yuesetting['no_weeks'];
		foreach($allgoods as $av){
			$av['mid']=$buymid;
			$av['merchid']=$merchid;
			$goodsset=$this->model('model')->check_goodsset($av);
			$numset=json_decode($goodsset['numset'],true);
			$lastnum=$av['num'];
			for($i=1;$i<=365;$i++){
				$daytime=$lastsendlog['daytime']+$i*24*3600;
				$week = date('w', $daytime);
				if(strpos($no_weeks,$week.';') !== false){
					continue;
				}
				$day=date('Y-m-d',$daytime);
				$daygoods=array('key'=>1);
				foreach($numset as $lkey=>$nv){
					$data=$nv['data'];
					if($lastnum<=0){
						break;
					}
					if($data[$week]['num']>0){
							$sendlog=$this->pdo_get('sendlog',array('day'=>$day,'buymid'=>$goodsset['mid'],'merchid'=>$goodsset['merchid']));
							$daygoods['goods']=json_decode($sendlog['goods'],true);
							if($data[$week]['num']>$lastnum){
								$data[$week]['num']=$lastnum;
							}
							$lastnum-=$data[$week]['num'];
							$isadd=1;
							$key=$goodsset['goodsid'].'_'.$goodsset['optionid'].'_'.$lkey;
							$daygoods['goods'][$key]=array(
									'num'=>$data[$week]['num'],
									'title'=>$goodsset['title'],
									'lkey'=>$lkey,
									'leititle'=>$nv['title'],
									'goodsid'=>$goodsset['goodsid'],
									'optionid'=>$goodsset['optionid'],
									'selespename'=>$goodsset['selespename'],
							);
					}
				}
				if($daygoods['goods']){
					$this->create_sendlog(0,$peisong_id,0,$day,1,$daygoods,$goodsset['mid'],$goodsset['merchid'],1);
				}
				if($lastnum<=0){
					break;
				}			
			}
		}
		return true;
	}
	/*
		uplog
	*/
	public function up_sendlog($order_id,$mid=0,$status=0,$peisong_id=0){
		$mid=$mid?:$_SESSION['uid'];
		if($peisong_id){
			$this->pdo_update('sendlog',array('mid'=>$peisong_id),array('buymid'=>$mid,'order_id'=>$order_id,'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
		}else{
			$this->pdo_update('sendlog',array('status'=>$status),array('buymid'=>$mid,'order_id'=>$order_id,'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
		}
		//完成日志
		if($status==2){
			$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('merchid','sendtype','mid','is_dingpei'));
			$list=pdo_getall('jy_peimilk_sendlog',array('buymid'=>$mid,'order_id'=>$order_id,'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
			foreach($list as $item){
				$this->create_orderlog($item['id'],'',$order['is_dingpei']);
			}
		}
		return true;
	}
	//获取可编辑的商品数量
	public function get_goodslognum($goodsset=array()){
		$mday=date("Y-m-d",strtotime("+1 day"));
		$mdaytime=strtotime($mday);		
		$goodsid=$goodsset['goodsid'];
		$optionid=$goodsset['optionid'];
		$isnum=$this->get_goodsnum($goodsset);
		//+配送中的
		$params=array('is_dingpei'=>1,'daytime <'=>$mdaytime,'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid'],'buymid'=>$goodsset['mid'],'merchid'=>$goodsset['merchid']);
		$list=pdo_getall('jy_peimilk_sendlog',$params);
		foreach($list as $item){
			$peisetgoods=json_decode($item['goods'],true);
			foreach($peisetgoods as $k=>$g){
				if($goodsid && $g['goodsid']==$goodsid && intval($g['optionid'])==$optionid){
					$isnum +=$g['num'];
				}
			}
		}
		$this->pdo_update('goodsset',array('lastnum'=>$goodsset['allnum']-$isnum),array('id'=>$goodsset['id'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$goodsset['merchid']));
		$res=array(
			'isnum'=>$isnum,
			'lastnum'=>$goodsset['allnum']-$isnum,
			'allnum'=>$goodsset['allnum'],
		);
		return $res;
	}
	public function get_goodsnum($order_goods=array()){
		$goodsset=$this->pdo_get('goodsset',array('optionid'=>$order_goods['optionid'],'goodsid'=>$order_goods['goodsid'],'mid'=>$order_goods['mid'],'merchid'=>$order_goods['merchid']));
		return $goodsset['issendnum'];
	}
	public function get_goodslastnum($mid=0,$merchid=0){
		$allgoods=$this->get_allgoods($mid,$merchid);
		$lastnum=0;
		foreach($allgoods as $av){
			$lastnum +=$av['lastnum'];
		}
		return $lastnum;
	}
	public function get_allgoods($mid=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$merchid=$merchid?:$_SESSION['onemerchid'];
		$allgoods=$this->pdo_getall('goodsset',array('mid'=>$mid,'merchid'=>$merchid));
		$params=array('istaocan'=>1,'ispay'=>1,'status'=>array(1,2,3,10,11,12,13),'mid'=>$mid,'merchid'=>$merchid);
		foreach($allgoods as $k=>&$av){
			$params['optionid']=$av['optionid'];
			$params['goodsid']=$av['goodsid'];
			$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('total','goodsnum'));
			$allnum=0;
			foreach($order_goods as $ov){
				$ov['goodsnum']=$ov['goodsnum']?:1;
				$ov['total']=$ov['total']?:1;
				$allnum +=$ov['goodsnum']*$ov['total'];
			}
			if($allnum<=0){
				unset($allgoods[$k]);
			}
			$av['allnum']=$allnum;
			$av['lastnum']=$av['allnum']-$av['issendnum'];
			$av['image']=pdo_getcolumn('jy_weishop_goods',array('id'=>$av['goodsid']),'image');
		}
		return $allgoods;
	}
	public function dingpei1($log_id,$day='',$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$nowlog=pdo_get('jy_peimilk_sendlog',array('id'=>$log_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$nowlog['id']){
			exi('该配送订单不存在！','error');
		}else{
			$this->pdo_update('sendlog',array('status'=>1,'day'=>$day),array('id'=>$log_id,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function dingpei2($log_id,$status=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->pdo_update('sendlog',array('status'=>2),array('id'=>$log_id,'uniacid'=>$_SESSION['uniacid']));
		$this->create_orderlog($log_id,0,1);
		return true;
	}
	public function sendlog1($logid,$day='',$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$nowlog=pdo_get('jy_peimilk_sendlog',array('id'=>$logid,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		if(!$nowlog['id']){
			exi('该配送订单不存在！','error');
		}
		$day=date('Y-m-d');
		if($nowlog['day'] !=$day){
			exi('只能发起当天的配送！','error');
		}else{
			$this->pdo_update('sendlog',array('status'=>1),array('id'=>$logid,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
			$this->model('notice')->notice_pei2buyer($nowlog);
		}
		return true;
	}
	public function sendlog2($logid,$status=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$nowlog=pdo_get('jy_peimilk_sendlog',array('id'=>$logid,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		if(!$nowlog['id']){
			exi('该配送订单不存在！','error');
		}else{
			$this->pdo_update('sendlog',array('status'=>2),array('id'=>$logid,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		}
		$this->create_orderlog($logid,$nowlog['mid'],1);
		return true;
	}
	/*
		创建结算订单
	*/
	function create_orderlog($logid,$mid=0,$is_dingpei=0,$get_money=0){
		$mid=$mid?:$_SESSION['uid'];
		//按商品/按件
		$sendlog=pdo_get('jy_peimilk_sendlog',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		$order=pdo_get('jy_weishop_order',array('id'=>$sendlog['order_id']),array('ordersn','price','merchid','is_dingpei'));
		$get_money=$this->model('model')->get_peifee($sendlog);
		$is_dingpei=$is_dingpei?:$sendlog['is_dingpei'];
		if($sendlog['is_dingpei']){
			$this->model('model')->up_goodsnum($sendlog);
		}
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$sendlog['mid']?:$mid,
				'merchid'=>$sendlog['merchid'],
				'ordersn'=>$order['ordersn'],
				'logid'=>$logid,
				'get_price'=>$get_money,
				'get_money'=>$get_money,
				'is_dingpei'=>$is_dingpei,
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		$this->insert('orderlog',$data);
		$this->model('notice')->notice_receive2buyer($sendlog);
		return true;
	}
	/*
		更新订单完成状态
	*/
	function up_ordergoods($mid=0,$merchid=0){
		pdo_update('jy_weishop_order_goods',array('status'=>3),array('status'=>array(1,2),'mid'=>$mid,'merchid'=>$merchid));
		pdo_update('jy_weishop_order',array('status'=>3),array('status'=>array(1,2),'ispay'=>1,'is_dingpei'=>1,'mid'=>$mid,'merchid'=>$merchid));
		return true;
	}
	/*
		报单
	*/
	function baodan_check($baodanid){
		//整理订单数据
		$post=pdo_get('jy_peimilk_baodan',array('id'=>$baodanid));
		$post['address']=json_decode($post['address'],true);
		$post['goods']=json_decode($post['goods'],true);
		$post['list']=$post['goods'];
		$post['total_price']=$post['goodsprice'];
		foreach($post['list'] as &$lv){
			$lv['goodsid']=$lv['id'];
			$api_data=pdo_getcolumn('jy_weishop_goods',array('id'=>$lv['goodsid']),'api_data');
			$api_data=json_decode($api_data,true);
			if($api_data['dingpei']){
				$post['is_dingpei']=1;
			}
			if($lv['optionid']){
				$lv['options']=pdo_get('jy_weishop_goods_option',array('goodsid'=>$lv['goodsid'],'id'=>$lv['optionid']));
			}
		}
		$_SESSION['order']=$post;
		$new_order=model('jy_weishop/get_order')->new_order($post);
		$new_order['mid']=$post['mid'];
		$new_order['plugin_name']='jy_peimilk';
		$new_order['status']=1;
		$new_order['is_dingpei']=$post['is_dingpei'];
		$order_id=model('jy_weishop/order')->create($new_order);
		pdo_update('jy_weishop_order',array('status'=>1,'ispay'=>1,'paytype'=>'apipay'),array('id'=>$order_id));
		pdo_update('jy_weishop_order_goods',array('status'=>1,'ispay'=>1),array('orderid'=>$order_id));
		if($post['peiset']){
			$peiset=json_decode($post['peiset'],true);
			//更新配送设置
			$data=array(
				'set_mid'=>$_SESSION['uid'],
				'days'=>trim($peiset['days']),
				'times'=>trim($peiset['times']),
				'goods'=>ijson_encode($peiset['goods']),
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$post['mid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['order_id']=$order_id;
			$data['createtime']=time();
			$this->insert('peiset',$data);
			//给客户设置配置
			foreach($peiset['goods'] as $gv){
				$gv['mid']=$post['mid'];
				$this->model('model')->check_goodsset($gv,'',true);
			}
		}
		if($post['peisong_id']){
			$peisong_id = pdo_getcolumn('jy_peimilk_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['peisong_id']),'mid');
			$params=array('order_id'=>$order_id,'peisong_id'=>$peisong_id);
			//判断配送员
			$this->order_get($params,'',true);
		}
		$this->order=model('jy_weishop/order')->get_order($order_id);
		$event=array(
			'type'=>'pay',
			'order'=>$this->order,
		);
		if($this->order['merchid'] && $this->plugin_exists('jy_merch')){
			model('jy_merch/model')->up_pay($this->order);
		}
		plugin('jy_weishop')->publish('order.change',$event);
		return true;
	}
	protected function upstoporder($orderid){
		$goodsorder=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
		foreach($goodsorder as $goods){
			$this->model('model')->check_goodsset($goods);
			$this->upallgoodssendlog($goods['mid'],$goods['merchid']);
		}
		return true;
	}
	/*
		订单升级为待结算
	*/
	public function cash_orderlog(){
		$cashdays=$this->setting['cash_day']?:1;
		$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
		$orderlog=pdo_getall('jy_peimilk_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'createtime <'=>$cashtime));
		foreach($orderlog as $item){
			pdo_update('jy_peimilk_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		return true;
	}
	/*
		待结算订单升级为结算完成/未结算
	*/
	public function change_orderlog($mid,$status=0){
		$orderlog=pdo_getall('jy_peimilk_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1));
		foreach($orderlog as $item){
			pdo_update('jy_peimilk_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status));
		}
		return true;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'remark'=>$remark,
		);
		$this->update('cashlog',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('cashlog',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$pay_type=$this->pay_types2[$pay_type];
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'applysn'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
		);
		if($pay_type){
			$update['paytype']=$pay_type;
		}
		$this->update('cashlog',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}